import { createStore } from "vuex";
import axios from "axios";

const store = createStore({
  state() {
    return {
      sellerIofo: {},
      goods: [],
      show: false,
      carList: [],
      price: 0,
    };
  },
  getters: {
    getPrice(state) {

    },
  },
  mutations: {
    setSeller(state, payload) {
      state.sellerIofo = payload.data;
    },
    setGoods(state, payload) {
      state.goods = payload.data;
    },
    showCar(state) {
      state.show = !state.show;
    },
    clearr(state) {
      state.carList = [];
      // console.log(state.goods)
      // console.log(state.goods[0].foods[0])
      for (let i = 0; i <= state.goods.length - 1; i++) {

        for (let j = 0; j <= state.goods[i].foods.length - 1; j++) {

          state.goods[i].foods[j].count = 0;
        }


      }

    },
    setCarList(state, payload) {



      const hasItem = state.carList.findIndex((item) => {


        return item.name == payload.item.name;
      });



      if (hasItem == "-1") {
        state.carList.push(payload.item);
        // console.log(state.carList)
      }
      console.log(state.carList.length)
    },
    // 减法
    setCarList1(state, payload) {


      if (payload.item.count === 0) {
        state.carList.length--
      }


    },
    setCarList2(state, payload) {


      if (payload.count === 0) {
        state.carList.length--
      }


    },
  },
  actions: {
    async getGoods({ commit }) {
      const {
        data: { data },
      } = await axios.get("/api/goods");
      commit("setGoods", { data });


    },
    async getSeller({ commit }) {
      const {
        data: { data },
      } = await axios.get("/api/seller");

      commit("setSeller", {
        data,
      });
    },
  },
});

export default store;
